package cn.itsource.laigou.service.impl;

import cn.itsource.laigou.domain.ProductType;
import cn.itsource.laigou.mapper.ProductTypeMapper;
import cn.itsource.laigou.service.IProductTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品目录 服务实现类
 * </p>
 *
 * @author zgf
 * @since 2019-08-20
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Override
    public List<ProductType> loadTree() {
        //几个级别的类型？ -- 不能确定
        List<ProductType> firstLevels = loadTreeFor_2();
        return firstLevels;
    }

    /**
     * 根据父类型的id查询所有子类型 -- 递归实现
     * @param pid
     * @return
     */
    public List<ProductType> loadTreeRecursive(Long pid){
        List<ProductType> productTypes = baseMapper.selectList(new QueryWrapper<ProductType>()
                .eq("pid", pid));
        if(productTypes==null||productTypes.size()==0){
            return null;
        }
        for (ProductType productType : productTypes) {
            List<ProductType> children = loadTreeRecursive(productType.getId());
            productType.setChildren(children);
        }
        return productTypes;
    }

    /**
     * 循环实现 -- 方式1
     * @return
     */
    public List<ProductType> loadTreeFor_1(){
        List<ProductType> productTypes = baseMapper.selectList(null);
        //新建一个List存放一级类型
        List<ProductType> fisrtLevelTypes = new ArrayList<>();

        for (ProductType productType : productTypes) {
            if(productType.getPid()==0){
                //如果是一级类型，则直接添加到fisrtLevelTypes
                fisrtLevelTypes.add(productType);
            }else{
                //如果不是一级类型，则添加到父类型的children属性中
                for (ProductType type : productTypes) {
                    if(productType.getPid()==type.getId()){
                        type.getChildren().add(productType);
                    }
                }
            }
        }

        return fisrtLevelTypes;
    }

    /**
     * 循环实现 -- 方式2
     * @return
     */
    public List<ProductType> loadTreeFor_2(){
        List<ProductType> productTypes = baseMapper.selectList(null);
        //新建一个List存放一级类型
        List<ProductType> fisrtLevelTypes = new ArrayList<>();

        //创建一个Map，存放所有的类型
        Map<Long,ProductType> map = new HashMap<>();
        for (ProductType productType : productTypes) {
            map.put(productType.getId(), productType);
        }

        //整理数据
        for (ProductType productType : productTypes) {
            if(productType.getPid()==0){
                fisrtLevelTypes.add(productType);
            }else{
                ProductType parentType = map.get(productType.getPid());
                parentType.getChildren().add(productType);
            }
        }

        return fisrtLevelTypes;
    }
}
